﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using UnityEngine;


public class ConfigBase<T> : SingletonTemplate<T> where T : new()
{
    protected virtual string GetConfigPath()
    {
        return null;
    }

    public virtual void Init()
    {
        LoadCSV();
    }

    protected virtual void LoadCSV()
    {
        var path = GetConfigPath();
        Debug.Log("[ConfigBase][LoadCSV] Load CSV  " + path);
        var text = Resources.Load<TextAsset>(path);
        if (text == null || text.bytes == null)
        {
            Debug.LogError("[ConfigBase][LoadCSV] Load CSV  Failed" + path);
            return;
        }
        var bytes = text.bytes;
        using (var reader = new StreamReader(new MemoryStream(bytes)))
        {
            var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
            OnCSVLoad(csv);
        }
    }

    protected virtual void OnCSVLoad(CsvReader csv)
    {
        while (csv.Read())
        {
            //todo;
        }
    }
}
